/*
 * Control - a graphical interface independent of display
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

// this code isn't used yet. It might be trashed or changed.

import flash3f.InputHandler;
import flash.Lib;
import flash.utils.Timer;
import flash.events.TimerEvent;

class Control
{

	public static var controls : Array<Control> = new Array();
	public static var pagetemplates : Array<Array<Bool>>;

	public var id : Int;
	public var active : Bool;
	public var tween : ControlTween;
	public var top : Float;
	public var left : Float;
	public var right : Float;
	public var bottom : Float;
	public var width : Float;
	public var height : Float;
	public var onMouseOver : Void->Void;
	public var onClick : Void->Void;
	public var onFrame : Void->Void;
	public var extra : Dynamic;

	#if flash9
		public var sprite : flash.display.Sprite;
	#end

	public function new(left : Float, top : Float, right : Float, bottom : Float, ?onmouse : Void->Void, ?onclick : Void->Void)
	{
		this.left = left;
		this.right = right;
		this.top = top;
		this.bottom = bottom;
		
		this.width = this.right - this.left;
		this.height = this.bottom - this.top;
		
		this.active = false;
		
		this.onMouseOver = onmouse;
		this.onClick = onclick;
		
		this.id = controls.length;
		controls.push(this);

		this.tween = null;

	}

	public function setWidth(width : Float)
	{
		this.width = width;
		this.right = this.left+width;
	}

	public function setHeight(height : Float)
	{
		this.height = height;
		this.bottom = this.top+height;		
	}
	
	public function setLeft(left : Float)
	{
		this.left = left;
		this.right = this.left+width;
		if (this.sprite!=null)
			this.sprite.x = this.left;
	}	

	public function setTop(top : Float)
	{
		this.top = top;
		this.bottom = this.top+height;		
		if (this.sprite!=null)
			this.sprite.y = this.top;
	}

	public function setRight(right : Float)
	{
		this.right = right;
		this.left = this.right-this.width;
		if (this.sprite!=null)
			this.sprite.x = this.left;
	}
	
	public function setBottom(bottom : Float)
	{
		this.bottom = bottom;
		this.top = this.bottom - this.height;
		if (this.sprite!=null)
			this.sprite.y = this.top;
	}

	public function show()
	{
		this.active = true;
		if (this.sprite!=null)
			this.sprite.visible = true;
	}

	#if flash9
	public function setSprite(sprite : flash.display.Sprite)
	{
		this.sprite = sprite;
		Lib.current.addChild(sprite);
	}
	#end

	public static function up()
	{
		var cx = InputHandler.cursx;
		var cy = InputHandler.cursy;
		
		for (c in controls)
		{

			if (c.onFrame!=null)
				c.onFrame();

			if (InputHandler.click)
			{
				if (c.active && cx>c.left && cx<c.right && cy>c.top && cy<c.bottom && c.onClick!=null)
				 {
					if (c.onClick!=null)
					c.onClick();	
				}
			}
			else
			{
				if (c.active && cx>c.left && cx<c.right && cy>c.top && cy<c.bottom && c.onMouseOver!=null)
				 {
					if (c.onMouseOver!=null)
					c.onMouseOver();	
				}
			}
			
			if (c.tween!=null)
				c.tween.up();
		}
				
	}
	
	public static function reset()
	{
	
		for (c in controls)
		{
			c.active = false;
			#if flash9
			if (c.sprite!=null)
				c.sprite.visible = false;
			#end
		}			
		
	}

	public static function start()
	{
		
	}

	public static function remove()
	{
		
	}

	public static var timer : Timer = new Timer(30,0);

	public static function main()
	{
		timer.start();

		var teststd = StandardControls.listTextButton(
		["StandardControls work","Button 2","Button 3","Large\nButton"], 
		"Vera Bold", 24,
		0xDDDDDD,
		0x888888, 0.75,
		0xDDDDDD, 0.75,
		0xFFFFFF, 0.75,
		2, 2, 6, 500);

		/*for (n in teststd)
		{
			new ControlTween(n);
			n.tween.endy = n.top+100;
			n.tween.transitionlength = 120;
			n.tween.transitiontype = "bounceinto";
		}*/

		timer.addEventListener(TimerEvent.TIMER, ComponentManager.up);
		ComponentManager.add(Control);
		InputHandler.start();
	}

}
